#extends Sprite2D
#
#var x = 0
#var y = 0
#
#@onready var Icon = %Icon
#var text;
#var 体节 = []
#var d
#var frame_count = 0
#var horizontal_step = 83  # 水平方向每格移动距离
#var vertical_step = 36    # 垂直方向每格移动距离
#var current_direction = Vector2.RIGHT  # 默认向右移动
#@onready var she_tou = %蛇头
#func _process(delta):
	#frame_count += 1
	#
	#if(Input.is_key_pressed(KEY_W)):
		#current_direction = Vector2.UP;
	#if(Input.is_key_pressed(KEY_S)):
		#current_direction = Vector2.DOWN;
	#if(Input.is_key_pressed(KEY_A)):
		#current_direction = Vector2.LEFT;
	#if(Input.is_key_pressed(KEY_D)):
		#current_direction = Vector2.RIGHT;
	#
	#if she_tou and self.position.distance_to(%Icon.position) < 5:    get_tree().paused = true
		#
	#if she_tou and self.position.distance_to(%Icon2.position) < 5:    get_tree().paused = true
		#
	#if she_tou and self.position.distance_to(%Icon3.position) < 5:    get_tree().paused = true
		#
	#if she_tou and self.position.distance_to(%Icon4.position) < 5:    get_tree().paused = true
	#
	#if frame_count >= 20:
		## 每60帧执行一次移动 
		#x = len(体节) # 重置计数器
		#while x > 1 :
			#体节[x-1].position = 体节[x-2].position
			#
			#
			#x -= 1
		#if (len(体节)>0):
			#体节[0].position=%身体2.position
		#%身体2.position = %身体.position
		#%身体.position = self.position 
		#%蛇头.position += current_direction * Vector2(horizontal_step, vertical_step)
		#frame_count = 0  # 重置计数器
		##while y < 100 :
			##y += 1
			##if self.position.distance_to(体节[y].position) < 5 ：
			##
			##if Icon and self.position.distance_to(Icon.position) < 5:
			##
			#
			#
			##y = 0 # 重置计数器
#
#
#
#
#
#
#
#
#
		#
#@onready var 身体 = %身体
#@onready var 苹果 = %苹果
#@onready var 身体2 = %身体2
#
#
#
				#
				#
				#
				#
				#
				#
##func _ready():
	##
#
#
		#
#
#
## 后续可以添加函数来改变移动方向
#func change_direction(new_direction):
	## 确保新方向不是当前方向的相反方向（贪吃蛇不能直接掉头）
	#if new_direction != -current_direction:
		#current_direction = new_direction
		#
		#
		#
		#
		#
		#
		#
		#
		#
		#
		#
		#
		#
#@onready var 边界节点 = [%Icon,%Icon2,%Icon3,%Icon4]
##(evnet):
extends Sprite2D

var x = 0
var y = 0

@onready var Icon = %Icon
var text
var 体节 = []
var d
var frame_count = 0
var horizontal_step = 83  # 水平方向每格移动距离
var vertical_step = 36    # 垂直方向每格移动距离
var current_direction = Vector2.RIGHT  # 默认向右移动
@onready var she_tou = %蛇头

func _process(delta):
	frame_count += 1
	
	# 输入处理
	if Input.is_key_pressed(KEY_W):
		current_direction = Vector2.UP
	if Input.is_key_pressed(KEY_S):
		current_direction = Vector2.DOWN
	if Input.is_key_pressed(KEY_A):
		current_direction = Vector2.LEFT
	if Input.is_key_pressed(KEY_D):
		current_direction = Vector2.RIGHT
	
		if she_tou:
			var 蛇头位置 = she_tou.position
			if (蛇头位置.x < 游戏区域_左边界 or 蛇头位置.x > 游戏区域_右边界 or 蛇头位置.y < 游戏区域_上边界 or 
				蛇头位置.y > 游戏区域_下边界):
				游戏失败("离开屏幕!")
	# 移动逻辑
	if frame_count >= 20:
		# 身体移动
		x = len(体节) # 重置计数器
		
		# 修复：添加大括号，并修正数组索引问题
		while x > 1:
			体节[x-1].position = 体节[x-2].position
			x -= 1
			
		if len(体节) > 0:
			体节[0].position = %身体2.position
			
		%身体2.position = %身体.position
		%身体.position = self.position 
		%蛇头.position += current_direction * Vector2(horizontal_step, vertical_step)
		frame_count = 0  # 重置计数器

# 节点引用放在最后
@onready var 身体 = %身体
@onready var 苹果 = %苹果
@onready var 身体2 = %身体2

# 改变方向函数
func change_direction(new_direction):
	if new_direction != -current_direction:
		current_direction = new_direction
		
# 边界节点数组
@onready var 边界节点 = [%Icon, %Icon2, %Icon3, %Icon4]






# 定义游戏区域的边界
var 游戏区域_左边界 = 50
var 游戏区域_右边界 = 1200
var 游戏区域_上边界 = 50
var 游戏区域_下边界 = 700

func 游戏失败()
